﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace QunYing.Common
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class OrderInfoSeveice : IOrderInfoService
    {
        public static OrderInfoSeveice Instance = new OrderInfoSeveice();

        public event Action<string, int> OrderInfoReceived;

        private System.Timers.Timer timer = new System.Timers.Timer();

        private Dictionary<string, OperationContext> contexts = new Dictionary<string, OperationContext>();

        private Dictionary<string, OperationContext> admin_contexts = new Dictionary<string, OperationContext>();

        private object obj = new object();

        public OrderInfoSeveice()
        {
            timer.AutoReset = false;
            timer.Interval = 1000 * 10;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();
            lock (obj)
            {
                foreach(var v in this.admin_contexts.Keys.ToArray())
                {
                    try
                    {
                        IWarnInfoHandle handle = this.admin_contexts[v].GetCallbackChannel<IWarnInfoHandle>();
                        handle.ProcessWarnInfo(-1);
                    }
                    catch (Exception ex)
                    {
                        admin_contexts.Remove(v);
                    }
                }
            }
            timer.Start();
        }

        #region IOrderInfoService 成员

        public void RegisterListener(string name,string otherInfo)
        {
            lock (obj)
            {
                    try
                    {
                       if (contexts.Keys.Contains(name))
                        {
                            contexts[name] = OperationContext.Current;
                        }
                        else
                        {
                            contexts.Add(name, OperationContext.Current);
                        }
                       QunYing.Common.Loger.Log.Error("注册订单信息处理时成功：" + name);
                    }
                    catch (Exception ex)
                    {
                        QunYing.Common.Loger.Log.Error("注册订单信息处理时发生了错误：" + ex.Message);
                    }
            }
        }

        public void UnregisterListener(string name, string otherInfo)
        {
            lock (obj)
            {
                try
                {
                    if (otherInfo != "经销商")
                    {
                        if (this.admin_contexts.Keys.Contains(name))
                        {
                            var v = this.contexts[name];
                            this.contexts.Remove(name);
                            v.Channel.Close();
                        }
                    }
                    else
                    {
                        if (this.contexts.Keys.Contains(name))
                        {
                            var v = this.contexts[name];
                            this.contexts.Remove(name);
                            v.Channel.Close();
                        }
                    }
                }
                catch(Exception ex)
                {
                }
            }
        }

        public void ProcessOrderInfo(string name, int warnInfoDataKey)
        {
            lock (obj)
            {
                try
                {
                    QunYing.Common.Loger.Log.Info("收到订单信息："+name);
                    if (this.OrderInfoReceived != null)
                        this.OrderInfoReceived(name, warnInfoDataKey);

                    if (this.contexts.Keys.Contains(name))
                    {
                        var temp = this.contexts[name];
                        IOrderInfoHandle handle = temp.GetCallbackChannel<IOrderInfoHandle>();
                        handle.ProcessOrderInfo(warnInfoDataKey);
                        Loger.Log.Info("回调成功订单信息！" + name);
                    }
                   
                }
                catch (Exception ex)
                {
                    contexts.Remove(name);
                    QunYing.Common.Loger.Log.Error("处理订单信息回调客户端出现异常：" + ex.Message);
                }
            }
        }

        #endregion
    }
}
